<?php
session_start();
include './source/class/class_core.php';

$admin_id = $_SESSION['user_id'];
$admin_name = $_SESSION['name'];

if(isset($_POST['action']) & $_POST['action'] == 'account'){
	$user_id = intval($_POST['user_id']);
	$user_name = trim($_POST['user_name']);
	$money = abs(intval($_POST['money']));
		
	DB::query("update om_user set money = money + $money where id = $user_id");
	DB::insert('account', array('admin_id' => $admin_id, 'admin_name' => $admin_name, 'user_id' => $user_id, 'user_name' => $user_name, 'money' => $money, 'op_time' => time()));
	echo '1';
	die;
}


?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>账户中心</title>
<link rel="stylesheet" href="images/global.min.css">
<script src="js/jquery.js" language="javascript" type="text/javascript"></script>
<script>
function account_order(user_id, user_name, money){
	$.ajax({
	   type: "POST",
	   url: "user_center.php",
	   data: "action=account&user_id=" + user_id + "&user_name=" + user_name + "&money=" + money,
	   success: function(msg){
		 if(msg == '1'){
			$('#btn_submit_' + user_id).val('结算成功');
		 }
	   }
	});
}
</script>
</head>
<body id="history">

<table>
	<tr>
		<td>用户名</td>
		<td>余额</td>
		<td>查看明细</td>
		<td></td>
	</tr>
	<?php 
	$source = DB::query("SELECT id, name, money FROM `om_user`");
	while ($row = DB::fetch($source)){
	?>
	<tr>
		<td><?=$row['name'] ?></td>
		<td><?=$row['money'] ?></td>
		<td><a href="order_detail.php?user_id=<?=$row['id'] ?>">查看</a></td>
		<td>
			<?php if($admin_name == 'MM' & $row['money'] < 0){ ?>
			<input type="button" id="btn_submit_<?=$row['id'] ?>" value="结算" onclick="this.disabled = 'true';account_order(<?=$row['id'] ?>, '<?=$row['name'] ?>', <?=$row['money'] ?>);" />
			<?php } ?>
		</td>
	</tr>
	<?php
	}
	?>
	
</table>